/*
 * Copyright 2023-2085 xujin <ceo@itgrail.com>
 *
 * Licensed under the Grail License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://itgrail.com/docs?id=3&catalogId=87
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.itgrail.pilot.app.event;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itgrail.grail.ddd.annotation.event.EventHandler;
import com.itgrail.grail.ddd.event.EventHandlerI;
import com.itgrail.grail.dto.Response;
import com.itgrail.grail.utils.reflect.BeanMapper;
import com.itgrail.pilot.domain.user.entity.UserE;
import com.itgrail.pilot.domain.user.event.CreateUserEvent;
import com.itgrail.pilot.domain.user.factory.DeptFactory;
import com.itgrail.pilot.domain.user.ports.UserRolePort;
import com.itgrail.pilot.domain.user.service.UserService;
import com.itgrail.pilot.infrastructure.tunnel.db.dao.UserMapper;
import com.itgrail.pilot.infrastructure.tunnel.db.dao.UserRolesMapper;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.UserDO;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.UserRolesDO;
import com.itgrail.pilot.infrastructure.tunnel.third.DingService;
import com.itgrail.pilot.infrastructure.tunnel.third.output.DingDept;
import com.itgrail.pilot.infrastructure.tunnel.third.output.DingUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.List;

@EventHandler
@Slf4j
public class CreateUserEventHandler implements EventHandlerI<Response, CreateUserEvent> {

    @Autowired
    private UserMapper mapper;

    @Autowired
    private UserService userService;

    @Autowired
    private DingService dingService;

    @Autowired
    private UserRolesMapper userRolesMapper;

    @Autowired
    private UserRolePort userRolePort;

    @Autowired
    private DeptFactory deptFactory;

    @Override
    public Response execute(CreateUserEvent createUserEvent) {
        log.info("创建OA用户信息:{}", createUserEvent.getAccount());
        QueryWrapper<UserDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account", createUserEvent.getAccount());
        UserDO userDO = mapper.selectOne(queryWrapper);
        String deptParentId = "";
        DingDept dingDept = new DingDept();
        if (null == userDO && StringUtils.isNotEmpty(createUserEvent.getMobile())) {
        } else {
        }
        if (StringUtils.isNotEmpty(deptParentId)) {
            List<DingUser> usersByDeptId = dingService.getUsersByDeptId(deptParentId);
            if (!CollectionUtils.isEmpty(usersByDeptId)) {
                usersByDeptId.forEach(x -> {
                    UserE userE = BeanMapper.map(x, UserE.class);
                    //判断是否已经存在
                    QueryWrapper<UserDO> dingUserDOQueryWrapper = new QueryWrapper<>();
                    dingUserDOQueryWrapper.eq("person_code", userE.getPersonCode());
                    Long integer = mapper.selectCount(dingUserDOQueryWrapper);
                    if (integer == 0) {
                        userService.addUser(userE);
                    }
                });
            }
        }
        //绑定用户角色 如果第一次进来  默认为USER
        UserRolesDO userRolesDO = userRolesMapper.queryByUserName(createUserEvent.getAccount());
        if (null == userRolesDO) {
            userRolePort.createUserRole(createUserEvent.getAccount(), "USER");
        }
        return Response.buildSuccess();
    }
}
